package com.aitaoxue.palx.ui;

import java.lang.ref.WeakReference;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.base.BaseActivity;
import com.aitaoxue.palx.db.SearchHistoryDBHelper;
import com.aitaoxue.palx.fragment.SearchHistoryFragment;
import com.aitaoxue.palx.fragment.SearchResultFragment;
import com.aitaoxue.palx.fragment.SearchResultFragment.OnSearchSuccessListener;
import com.aitaoxue.palx.util.StringUtils;
import com.aitaoxue.palx.widget.ClearEditText;

public class SearchActivity extends BaseActivity implements
		OnSearchSuccessListener {

	// 搜索内容
	private ClearEditText searchContent;
	// 搜索按钮
	private Button search;

	private FragmentTransaction trans;

	private SearchResultFragment searchResultFragment;
	private SearchHistoryDBHelper dbHelper;
	private SQLiteDatabase db;

	// 标志量标记正在搜索
	private boolean searching;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_search;
	}

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected boolean hasActionBar() {
		return false;
	}

	@Override
	protected int getActionBarCustomView() {
		return R.layout.actionbar_search;
	}

	@Override
	protected void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);

		trans = getSupportFragmentManager().beginTransaction();
		trans.replace(R.id.container, new SearchHistoryFragment());

		// trans.replace(R.id.container, new SearchResultFragment());
		trans.commit();
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.search:
			if (StringUtils.isEmpty(searchContent.getText().toString())) {
				showToast("请输入搜索内容", 0, 0);
			} else {
				searching = true;

				if (searchResultFragment == null) {
					trans = getSupportFragmentManager().beginTransaction();
					searchResultFragment = new SearchResultFragment();
					trans.replace(R.id.container, searchResultFragment)
							.commit();
				} else {
					searchResultFragment.onSearchChanged();
				}

			}
			break;
		default:
			break;
		}
	}

	@Override
	public void initView() {
		searchContent = (ClearEditText) findViewById(R.id.search_content);
		search = (Button) findViewById(R.id.search);
	}

	@Override
	public void initData() {
		search.setOnClickListener(this);
	}

	@Override
	public void searchSuccess() {
		if (searching) {
			searching = false;

			new SaveHistoryTask(this).execute(searchContent.getText()
					.toString());
		}
	}

	private class SaveHistoryTask extends AsyncTask<String, Void, Void> {

		private WeakReference<Context> mContext;

		private SaveHistoryTask(Context context) {
			mContext = new WeakReference<Context>(context);
		}

		@Override
		protected Void doInBackground(String... params) {
			if (dbHelper == null) {
				dbHelper = new SearchHistoryDBHelper(mContext.get());
			}

			db = dbHelper.getWritableDatabase();

			Cursor cursor = db.rawQuery(
					"select * from search_history where content=" + "\""
							+ params[0] + "\"", null);

			int newTime = (int) System.currentTimeMillis();
			if (cursor.getCount() > 0) {
				// 存在
				db.execSQL("update search_history set time=" + newTime
						+ " where content=" + "'" + params[0] + "'");
			} else {
				// 不存在
				String test="insert into search_history (content,time) values ('"
						+ params[0] + "'," + newTime + ")";
				db.execSQL("insert into search_history (content,time) values ('"
						+ params[0] + "'," + newTime + ")");
			}

			cursor.close();
			db.close();

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
		}
	}

}
